<!--
 * @Description: 订单通知
 * @Author: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @Date: 2021-07-03 11:41:05
 * @LastEditTime: 2023-08-03 15:52:10
 * @LastEditors: DXV-RGWU-TUFH-RFCY-IEGMYY
-->

<template>
  <div class="lb-shop-notice">
    <top-nav />
    <div class="page-main">
      <el-row class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="通知类型" prop="type">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.type"
              placeholder="请选择"
            >
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="阅读状态" prop="have_look">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.have_look"
              placeholder="请选择"
            >
              <el-option
                v-for="item in statusList"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column
          prop="order_id"
          label="订单ID"
          width="120"
        ></el-table-column>
        <el-table-column prop="type" label="通知类型">
          <template slot-scope="scope">
            {{ scope.row.type | handleNoticType(typeList) }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="消息内容" min-width="200">
          <template slot-scope="scope">
            <div class="flex-y-center">
              {{ noticeTypeText[scope.row.type] }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="推送时间" min-width="120">
        </el-table-column>
        <el-table-column prop="have_look" label="阅读状态">
          <template slot-scope="scope">
            {{ scope.row.have_look | handleNoticType(statusList) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="120">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="goNoticeItem(scope.row)"
                v-hasPermi="
                  scope.row.type === 3
                    ? `ShopRefuseOrder-pagedata`
                    : scope.row.type === 2
                    ? scope.row.is_add
                      ? `ShopBellRefund-view`
                      : `ShopRefund-view`
                    : scope.row.is_add
                    ? `ShopBellOrder-view`
                    : `ShopOrder-view`
                "
                v-show="$route.meta.pagePermission[0].auth.includes('read')"
                >{{ $t('action.view') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDel(scope.row.id)"
                v-show="scope.row.have_look === 1"
                v-hasPermi="`${$route.name}-delete`"
                >{{ $t('action.delete') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="success"
                @click="updateItem(scope.row.id, 1)"
                v-show="scope.row.have_look === 0"
                v-hasPermi="`${$route.name}-read`"
                >{{ $t('action.read') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import moment from 'moment'
export default {
  data () {
    return {
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > (moment(moment(Date.now()).format('YYYY-MM-DD')).unix() + 24 * 3600 - 1) * 1000
        }
      },
      typeList: [],
      statusList: [{ id: -1, title: '全部' }, { id: 0, title: '未读' }, { id: 1, title: '已读' }],
      noticeTypeText: {
        1: `你有一笔新的订单，请及时联系${this.$t('action.attendantName')}处理`,
        2: `你有一笔新的退款订单，请及时处理`,
        3: `您有一笔新的订单，已被${this.$t('action.attendantName')}拒绝，请在后台及时处理转单，避免平台订单流失`,
        4: `你有一笔新的订单，${this.$t('action.attendantName')}长时间未接单，请联系${this.$t('action.attendantName')}及时接单`,
        5: `你有一笔新的订单有迟到风险，请跟进${this.$t('action.attendantName')}是否达到目的地`,
        6: `监测到有${this.$t('action.attendantName')}完成服务后，未离开目的地，请联系${this.$t('action.attendantName')}询问具体情况，如遇安全问题，请及时报警，如是跳单情况，根据平台规则自行处理`
      },
      loading: false,
      searchForm: {
        page: 1,
        limit: 10,
        start_time: '',
        end_time: '',
        type: 0,
        have_look: -1
      },
      tableData: [],
      total: 0
    }
  },
  created () {
    let { ShopOrderPage, ShopBellOrderPage, ShopRefundPage, ShopBellRefundPage, ShopRefuseOrderPage } = this.routesItem
    let arr = [{ id: 0, title: '全部' }, { id: 1, title: '来单通知' }, { id: 2, title: '退款通知' }, { id: 3, title: '拒单通知' }, { id: 4, title: '未接单通知' }, { id: 5, title: '服务迟到通知' }, { id: 6, title: '跳单预警通知' }]
    arr.map(item => {
      if ((!ShopOrderPage && !ShopBellOrderPage && [1, 4, 5, 6].includes(item.id)) || (!ShopRefundPage && !ShopBellRefundPage && item.id === 2) || (!ShopRefuseOrderPage && item.id === 3)) {
        item.is_hide = true
      }
    })
    let typeList = arr.filter(item => {
      return !item.id || !item.is_hide
    })
    this.typeList = typeList
  },
  activated () {
    this.getTableDataList()
  },
  computed: {
    ...mapState({
      routesItem: state => state.routes
    })
  },
  methods: {
    ...mapMutations(['changeRoutesItem']),
    resetForm (form) {
      this.$refs[form].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.tableData = []
      this.loading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      let { start_time: time } = searchForm
      if (time && time.length > 0) {
        searchForm.start_time = time[0] / 1000
        searchForm.end_time = time[1] / 1000
      } else {
        searchForm.start_time = ''
        searchForm.end_time = ''
      }
      if (searchForm.have_look === -1) {
        delete searchForm.have_look
      }
      let { code, data } = await this.$api.shop.noticeList(searchForm)
      this.loading = false
      if (code !== 200) return
      this.tableData = data.data
      this.total = data.total
    },
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, -1)
      }).catch(() => { })
    },
    async updateItem (id, status, show = true) {
      let param = { id, have_look: status }
      if (status === 1) {
        param.is_pop = 1
      }
      this.$api.shop.noticeUpdate(param).then((res) => {
        if (res.code === 200) {
          if (status === 1) {
            this.changeRoutesItem({ key: 'notice_num', val: this.routesItem.notice_num - 1 })
          }
          if (show) {
            this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          }
          if (status === -1) {
            this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
          }
          this.getTableDataList()
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    },
    async goNoticeItem (item) {
      let { id, type, order_id: oid = 0, have_look: status = 0 } = item
      if (!status) {
        this.updateItem(id, 1, false)
      }
      let url = type === 2 ? `/shop/refund/detail?id=${oid}` : type === 3 ? `/shop/order/refuse` : `/shop/order/detail?id=${oid}`
      this.$router.push(url)
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    },
    handleNoticType (type, datas) {
      let arr = datas.filter(item => {
        return item.id === type
      })
      return arr.length > 0 ? arr[0].title : ''
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-shop-notice {
  .page-main {
    width: 100%;

    .el-select,
    .el-input-number,
    .el-input {
      width: 200px;
    }
  }
}

.none {
  display: none;
}
</style>
<style media="print">
.none {
  display: block;
}
</style>
